import {imageUrl} from "../../utils/imageutil"
Component({
  properties: {
    show:{
      type:Boolean,
      value:false
    },
    defaultStr: {
      type: String,
      value: ''
    },
    plateNum: {
      type: String,
      value: ''
    },
  },
  data: {
    imageUrl,
    inputList: ["", "", "", "", "", "", "", ""],
    curInput:0,
    maxNum: 8,
    showKeyPop1: false,
    showKeyPop2: false,
    keyProvince1: ['京', '津', '晋', '冀', '蒙', '辽', '吉', '黑', '沪'],
    keyProvince2: ['苏', '浙', '皖', '闽', '赣', '鲁', '豫', '鄂', '湘'],
    keyProvince3: ['粤', '桂', '琼', '渝', '川', '贵', '云', '藏'],
    keyProvince4: ['陕', '甘', '青', '宁', '新', 'W'],
    keyEnInput1: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
    keyEnInput2: ["Q", "W", "E", "R", "T", "Y", "U", "P", "学", "军"],
    keyEnInput3: ["A", "S", "D", "F", "G", "H", "J", "K", "L", "警"],
    keyEnInput4: ["Z", "X", "C", "V", "B", "N", "M", "港", "澳"],
    lockInput: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
  },
  lifetimes: {
    ready: function () {
      if (this.data.defaultStr != "" && this.data.defaultStr != null) {
        const valList = this.data.defaultStr.split("")
        for (let i in valList) {
          this.setData({
            ['inputList[' + i + ']']: valList[i]
          })
        }
      }
    },
  },
  observers: {
    defaultStr(val) {
      if (val != "" && val != null) {
        const valList = val.split("")
        valList.forEach((v,i)=>{
          this.setData({
            ['inputList[' + i + ']']: valList[i]
          })
        })
      }
    },
    curInput(val) {
      this.showOrHidePop(val)

      this.data.keyEnInput2 = ["Q", "W", "E", "R", "T", "Y", "U", "O", "P", "军"]
      switch (val) {
        case 1:
          this.setData({
            lockInput: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "学", "军", "警", "港", "澳"]
          })
          break
        case 2:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        case 3:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        case 4:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        case 5:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        case 6:
          this.setData({
            lockInput: ["O"],
            keyEnInput2: ["Q", "W", "E", "R", "T", "Y", "U", "P", "学", "军"]
          })
          break
        case 7:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        default:
          this.setData({
            lockInput: []
          })
          break
      }
    }
  },
  methods: {
    change_curInput(val) {
      this.setData({
        curInput:val
      })
      this.showOrHidePop(val)

      this.data.keyEnInput2 = ["Q", "W", "E", "R", "T", "Y", "U", "O", "P", "军"]
      switch (val) {
        case 1:
          this.setData({
            lockInput: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "学", "军", "警", "港", "澳"]
          })
          break
        case 2:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        case 3:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        case 4:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        case 5:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        case 6:
          this.setData({
            lockInput: ["O"],
            keyEnInput2: ["Q", "W", "E", "R", "T", "Y", "U", "P", "学", "军"]
          })
          break
        case 7:
          this.setData({
            lockInput: ["O", "学", "军", "警", "港", "澳"]
          })
          break
        default:
          this.setData({
            lockInput: []
          })
          break
      }
    },
    plateInput(e) {
      let index = e.currentTarget.dataset.index
      this.setData({
        curInput: index
      })
      this.showOrHidePop(index)
    },
    showOrHidePop(val) {
      if (val == -1) {
        this.setData({
          showKeyPop1: false,
          showKeyPop2: false
        })
      } else if (val == 0) {
        this.setData({
          showKeyPop1: true,
          showKeyPop2: false
        })
      } else {
        this.setData({
          showKeyPop1: false,
          showKeyPop2: true
        })
      }
    },
    tapKeyboard(e) {
      let item = e.currentTarget.dataset.item
      if (this.data.lockInput.includes(item)) {
        return
      }

      this.setData({
        ['inputList['+this.data.curInput+']']:item
      })
      if (this.data.curInput < this.data.maxNum - 1) {
        this.setData({
          curInput: this.data.curInput + 1
        })
      } else {
        // this.setData({
        //   curInput: -1
        // })
      }

      // this.emitResult()
    },
    closeKeyboard() {
      this.setData({
        curInput: -1,
        show:false
      })
    },
    onPlateDelTap() {
      if (this.data.inputList[this.data.curInput] == " ") {
        this.setData({
          curInput: this.data.curInput - 1
        })
      }
      this.data.inputList[this.data.curInput] = " "

      // this.emitResult()
    },
    emitResult() {
      const returnResult = this.data.inputList.join("");
      let result_arr = returnResult.split('');
      if(this.data.inputList[7]){
        if(result_arr.length==8){
          this.closeKeyboard()
          this.triggerEvent('car_num', returnResult)
        }else{
          getApp().showToast('请输入正确的车牌号')
        }
      }else{
        if(result_arr.length==7){
          this.closeKeyboard()
          this.triggerEvent('car_num', returnResult)
        }else{
          getApp().showToast('请输入正确的车牌号')
        }
      }
    }
  }
});